package org.jeecg.trace.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.jeecg.trace.entity.MesMaterialLotHistory;
import org.jeecg.trace.service.IMesMaterialLotHistoryService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.trace.vo.DictVO;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 原材料批次历史
 * @Author: LiCY
 * @Date:   2024-06-18
 * @Version: V1.0
 */
@Api(tags="原材料批次历史")
@RestController
@RequestMapping("/trace/mesMaterialLotHistory")
@Slf4j
public class MesMaterialLotHistoryController extends JeecgController<MesMaterialLotHistory, IMesMaterialLotHistoryService> {
	@Autowired
	private IMesMaterialLotHistoryService mesMaterialLotHistoryService;
	
	/**
	 * 分页列表查询666
	 *
	 * @param mesMaterialLotHistory
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "原材料批次历史-分页列表查询")
	@ApiOperation(value="原材料批次历史-分页列表查询", notes="原材料批次历史-分页列表查询")
	@GetMapping(value = "/list")
	@DS("history")
	public Result<IPage<MesMaterialLotHistory>> queryPageList(MesMaterialLotHistory mesMaterialLotHistory,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<MesMaterialLotHistory> queryWrapper = QueryGenerator.initQueryWrapper(mesMaterialLotHistory, req.getParameterMap());
		Page<MesMaterialLotHistory> page = new Page<MesMaterialLotHistory>(pageNo, pageSize);
		queryWrapper.orderByDesc("CTIME");
		IPage<MesMaterialLotHistory> pageList = mesMaterialLotHistoryService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 根据产线工序查询原材料下拉列表
	  *
	  * @param line
	  * @param op
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "原材料批次历史-分页列表查询")
	 @ApiOperation(value="原材料批次历史-分页列表查询", notes="原材料批次历史-分页列表查询")
	 @GetMapping(value = "/getMaterialOptions")
	 @DS("history")
	 public Result<List<DictVO>> getMaterialOptions(
												@RequestParam(name="line") String line,
												@RequestParam(name="op") String op,
												HttpServletRequest req) {
		 List<DictVO> list = mesMaterialLotHistoryService.getMaterialOptions(line, op);
		 return Result.OK(list);
	 }
	
	/**
	 *   添加
	 *
	 * @param mesMaterialLotHistory
	 * @return
	 */
	@AutoLog(value = "原材料批次历史-添加")
	@ApiOperation(value="原材料批次历史-添加", notes="原材料批次历史-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:mes_material_lot_history:add")
	@PostMapping(value = "/add")
	@DS("business")
	public Result<String> add(@RequestBody MesMaterialLotHistory mesMaterialLotHistory) {
		mesMaterialLotHistoryService.save(mesMaterialLotHistory);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param mesMaterialLotHistory
	 * @return
	 */
	@AutoLog(value = "原材料批次历史-编辑")
	@ApiOperation(value="原材料批次历史-编辑", notes="原材料批次历史-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:mes_material_lot_history:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	@DS("business")
	public Result<String> edit(@RequestBody MesMaterialLotHistory mesMaterialLotHistory) {
		mesMaterialLotHistoryService.updateById(mesMaterialLotHistory);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过选中对象删除
	 *
	 * @param mesMaterialLotHistory
	 * @return
	 */
	@AutoLog(value = "原材料批次历史-通过id删除")
	@ApiOperation(value="原材料批次历史-通过id删除", notes="原材料批次历史-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:mes_material_lot_history:delete")
	@RequestMapping(value = "/delete")
	public Result<String> delete(@RequestBody MesMaterialLotHistory mesMaterialLotHistory) {
		mesMaterialLotHistoryService.removeById(mesMaterialLotHistory);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param list
	 * @return
	 */
	@AutoLog(value = "原材料批次历史-批量删除")
	@ApiOperation(value="原材料批次历史-批量删除", notes="原材料批次历史-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:mes_material_lot_history:deleteBatch")
	@RequestMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestBody List<MesMaterialLotHistory> list) {
	    list.forEach(item->{
    			mesMaterialLotHistoryService.removeById(item);
    		});
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "原材料批次历史-通过id查询")
	@ApiOperation(value="原材料批次历史-通过id查询", notes="原材料批次历史-通过id查询")
	@GetMapping(value = "/queryById")
	@DS("history")
	public Result<MesMaterialLotHistory> queryById(@RequestParam(name="id",required=true) String id) {
		MesMaterialLotHistory mesMaterialLotHistory = mesMaterialLotHistoryService.getById(id);
		if(mesMaterialLotHistory==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(mesMaterialLotHistory);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param mesMaterialLotHistory
    */
    //@RequiresPermissions("org.jeecg.modules.demo:mes_material_lot_history:exportXls")
    @RequestMapping(value = "/exportXls")
    @DS("history")
    public ModelAndView exportXls(HttpServletRequest request, MesMaterialLotHistory mesMaterialLotHistory) {
        return super.exportXls(request, mesMaterialLotHistory, MesMaterialLotHistory.class, "原材料批次历史");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("mes_material_lot_history:importExcel")
    @DS("history")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MesMaterialLotHistory.class);
    }

}
